package sdk.webview.fmc.com.fmcsdk.activity;

import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.UploadLogAdapter;
import sdk.webview.fmc.com.fmcsdk.interfaces.UploadLogView;
import sdk.webview.fmc.com.fmcsdk.presenter.UploadLogPresenter;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.FileUtil;
import sdk.webview.fmc.com.fmcsdk.util.SdkVersionUtils;
import sdk.webview.fmc.com.fmcsdk.util.TitleBar;
import sdk.webview.fmc.com.fmcsdk.view.LoginFrameStyleColorButton;

/*日志上传*/
public class UploadLogActivity extends BaseActivity<UploadLogView, UploadLogPresenter> implements UploadLogView, UploadLogAdapter.CheckBoxCallBack, View.OnClickListener {
    List<Map<String, Object>> filesList;
    ListView listview;
    LoginFrameStyleColorButton ok_btn;
    TitleBar titlebar;
    private UploadLogAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_upload_log;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        listview = (ListView) findViewById(R.id.listview);
        ok_btn = (LoginFrameStyleColorButton) findViewById(R.id.floating);
        ok_btn.setOnClickListener(this);
        titlebar = (TitleBar) findViewById(R.id.titlebar);
        titlebar.setTitle(getString(R.string.upload_log));
        //显示日志列表
        updateDate();

        ok_btn.setOnClickListener(this);
    }

    //显示日志列表
    void updateDate() {
        String storePath;
        if (SdkVersionUtils.checkedAndroid_R()) {
            storePath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");
        } else {
            storePath = context.getFilesDir().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");

        }
//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");
        File logfile = new File(storePath + Constant.LOGPATH);
        filesList = new ArrayList();
        if (logfile.exists()) {
            File files[] = logfile.listFiles();
            for (File file : files) {
                String name = file.getName();
                if (name.matches("^(\\d+)(.*)") && name.endsWith(".log")) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("filename", name);
                    map.put("ischeck", false);
                    map.put("size", FileUtil.formatFileSize(file.length()));
                    filesList.add(map);
                }
            }
            adapter = new UploadLogAdapter(context, filesList, R.layout.uploadlog_item, this);
            listview.setAdapter(adapter);
        }
    }

    @Override
    protected UploadLogPresenter createPresenter() {
        return new UploadLogPresenter();
    }


    @Override
    public void selectCheckBox(boolean isChecked, int position) {

        filesList.get(position).put("ischeck", isChecked);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.floating) {
            for (int i = 0; i < filesList.size(); i++) {
                if ((boolean) filesList.get(i).get("ischeck")) {
                    presenter.uploadLog((String) filesList.get(i).get("filename"), i);
                }
            }
        }
    }

    @Override
    public void uploadLogSuccess(File files, int i) {
        ToastMessage(R.string.upload_success);
        files.delete();
        filesList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
